package com.heima.service.impl;

import com.heima.mapper.UserMapper;
import com.heima.pojo.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginUserVo login(LoginDto dto) {

        if(dto == null) {
            throw new RuntimeException("非法参数");
        }

        //1.根据查询用户名查询
        User user = userMapper.findByLoginName(dto.getLoginName());
        if(user == null) {
            throw new RuntimeException("账号不存在，请联系管理员！");
        }

        //2.判断密码
        if(!user.getPassword().equals(dto.getPassword())) {
            throw new RuntimeException("账号或密码错误！");
        }

        LoginUserVo vo = new LoginUserVo();
        vo.setUserId(user.getId());
        vo.setUserName(user.getUserName());
        vo.setRoleCode(user.getRoleCode());
        vo.setSuccess(true); //是否成功
        vo.setMsg("登录成功");
        vo.setToken(dto.getClientToken());
        vo.setRepair("维修员".equals(user.getUserName()));

        //areaMapper.findById(user.getAreaId());
        vo.setRegionId("杭州");

        return vo;
    }
}
